package com.demo.test.other;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 时间字符串转换为时间戳类 用于转换hbase中的时间字符串
 * 
 * @author Administrator
 *
 */

public class DateUtil {
	private static String[] pattern = new String[] { "yyyy-MM", "yyyyMM", "yyyy/MM", "yyyyMMdd", "yyyy-MM-dd",
			"yyyy/MM/dd", "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy-MM-dd HH:mm",
			"yyyy.MM.dd", "yyyy年MM月dd日" };

	public static Date getNow(String format) throws Exception {
		return string2Date(getNowStr(format));
	}

	public static String getNowDateStr() {
		return date2String(new Date(), "yyyy-MM-dd");
	}


	public static String getNowStr(String format) {
		Calendar cal = Calendar.getInstance();
		Date date = cal.getTime();
		return date2String(date, format);
	}


	public static Date string2Date(String dateStr) throws Exception {
		if (StringUtils.isEmpty(dateStr)){
			return null;
		}
		Date date = org.apache.commons.lang.time.DateUtils.parseDate(dateStr, pattern);
		return date;
	}


	public static String date2String(Date date, String format) {
		if (date == null){
			return "";
		}
		if (format == null || "".equals(format)){
			format = "yyyy-MM-dd HH:mm:ss";
		}
		DateFormat formater = new SimpleDateFormat(format);
		return formater.format(date);
	}


	/**
	 * afterDate日期是否在beforeDate日期之后
	 * 
	 * @param afterDate
	 * @param beforeDate
	 * @return
	 */
	public static boolean isAfter(Date afterDate, Date beforeDate) {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(afterDate);
		Calendar c2 = Calendar.getInstance();
		c2.setTime(beforeDate);
		return c1.after(c2);
	}

	/**
	 * date1日期与date2日期是否相等
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isEqual(Date date1, Date date2) {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date1);
		Calendar c2 = Calendar.getInstance();
		c2.setTime(date2);
		return c1.equals(c2);
	}

	/**
	 * 获取当前日期 yyyy-mm-dd
	 * 
	 * @param
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String NowDate() {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateNow = sdf.format(d);
		return dateNow;
	}


	/**
	 * 获取前七天日期
	 */
	@SuppressWarnings("rawtypes")
	public static List<String> getSevenDay() throws ParseException {
		Date dateNow = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String datestr = sdf.format(new Date());
		Date date = sdf.parse(datestr);
		Calendar curr = Calendar.getInstance();
		curr.setTime(date);
		curr.add(Calendar.DATE, 1);
		ArrayList<String> arrayList = new ArrayList<String>();
		Calendar start = Calendar.getInstance();
		start.setTime(date);
		start.add(Calendar.DATE, -6);
		while (start.before(curr)) {
			arrayList.add(sdf.format(start.getTime()));
			start.add(Calendar.DATE, 1);
		}

		return arrayList;
	}



	public static List<String> get24Hour(String realTime) throws ParseException {
		ArrayList<String> arrayList = new ArrayList<String>();
		int hourInt = 24;
		for (int i = 0; i < hourInt; i++) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
			Date parse = sdf.parse(realTime + " 23");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(parse);
			calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - i);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");

			String hour = df.format(calendar.getTime());
			arrayList.add(hour);
		}
		Collections.reverse(arrayList);
		return arrayList;
	}



	@SuppressWarnings("deprecation")
	public static String NowForHour() {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("HH");
		String dateNow = sdf.format(d);
		return dateNow;
	}

	

	public static String beforeWeekToday() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE, -7);
		Date d = c.getTime();
		String day = format.format(d);
		return day;
	}


	public static String beforeMonthToday() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MONTH, -1);
		Date m = c.getTime();
		String mon = format.format(m);
		return mon;
	}

	public static Date beforeTowHour(String date) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(date));
		cal.add(Calendar.HOUR_OF_DAY, -2);
		return cal.getTime();
	}


	
}
